package com.base.essearch.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.essearch.es.bean.MovieEntity;
import com.base.essearch.es.bean.PageEntity;
import com.base.essearch.es.service.IMovieService;
import com.base.essearch.video.entity.VideoMediaFile;
import com.base.essearch.video.model.Video;
import com.base.essearch.video.service.IVideoMediaFileService;
import com.base.essearch.webupload.entity.FileRecord;
import com.base.essearch.webupload.service.IFileRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lr
 * @since 2020-12-06
 */
@Slf4j
@RestController
public class VideoMediaFileController {
    @Autowired
    private IVideoMediaFileService videoMediaFileService;
    @Autowired
    private IFileRecordService fileRecordService;
    @Autowired
    private IMovieService movieService;

    @RequestMapping("ajax_list-videos.html")
    public Object video(String menu, String search, String selectType, int pageNum, int pageSize) {
        log.info(menu);
        log.info(search);
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<VideoMediaFile> in = Wrappers.<VideoMediaFile>lambdaQuery()
                .like(VideoMediaFile::getTag, menu);
        List<VideoMediaFile> list = videoMediaFileService.list("all".equalsIgnoreCase(menu) || "*".equals(menu) ? null : in);
        PageInfo<VideoMediaFile> info = new PageInfo<>(list);
        if (StringUtils.isNotBlank(search)) {
            PageEntity mapPage = movieService.getAllMovieForPage(menu, search, selectType, pageNum, pageSize, true);
            list = transferList(mapPage, info);
        }
        List<String> md5s = list.stream().map(VideoMediaFile::getFileId).collect(Collectors.toList());
        PageInfo<Video> videos = new PageInfo<>();
        if (md5s.size() < 1) {
            return videos;
        }
        List<FileRecord> fileRecords = fileRecordService.list(Wrappers.<FileRecord>lambdaQuery()
                .in(FileRecord::getMd5Value, md5s)
        );

        List<FileRecord> imgs = fileRecordService.list(Wrappers.<FileRecord>lambdaQuery()
                .in(FileRecord::getId, fileRecords.stream().map(FileRecord::getImgId).collect(Collectors.toList()))
        );
        Map<String, List<FileRecord>> mapD5 = fileRecords.stream().collect(Collectors.groupingBy(FileRecord::getMd5Value));
        Map<String, List<FileRecord>> imgsMap = imgs.stream().collect(Collectors.groupingBy(FileRecord::getId));


        BeanUtils.copyProperties(info, videos);
        List<Video> ss = new ArrayList<>();
        for (VideoMediaFile file : list) {
            Video v = new Video();
            List<FileRecord> fileRecords1 = mapD5.get(file.getFileId());
            if (fileRecords1.size() > 0) {
                v.setMenu(file.getMimeType());
                v.setMenuId(file.getFileType());
                FileRecord fileRecord = fileRecords1.get(0);
                List<FileRecord> fileRecords2 = imgsMap.get(fileRecord.getImgId());
                String img = fileRecords2 != null ? fileRecords2.get(0).getNetworkPath() : "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1438277128,1176916990&fm=26&gp=0.jpg";
                v.setHot(file.getFileType());
                v.setPicImg(img);
                v.setVideoFen("5");
                v.setVideoTitle(StringUtils.isBlank(fileRecord.getSearchKey()) ? file.getFileOriginalName() : fileRecord.getSearchKey());
                if (StringUtils.isNotBlank(search)) {
                    v.setVideoTitle(file.getFileOriginalName());
                }
                v.setVideoUrl(file.getFileUrl());
                v.setFileId(file.getFileId());
                ss.add(v);
            }
        }
        videos.setList(ss);

        return videos;
    }

    private List<VideoMediaFile> transferList(PageEntity mapPage, PageInfo<VideoMediaFile> info) {
        info.setPageSize(mapPage.getPageSize());
        info.setPageNum(mapPage.getPage());
        info.setTotal(mapPage.getTotalCount());
        List<MovieEntity> list = (List<MovieEntity>) mapPage.getDataList();

        List<String> ids = new ArrayList<>();
        for (MovieEntity entity : list) {
            ids.add(entity.getMovieId());
        }
        List<VideoMediaFile> idLists = new ArrayList<>();
        if (ids.size() > 0) {
            idLists = videoMediaFileService.listByIds(ids);
        }

        Map<String, List<VideoMediaFile>> idMaps = idLists.stream().collect(Collectors.groupingBy(VideoMediaFile::getFileId));
        for (MovieEntity entity : list) {
            List<VideoMediaFile> oneRowList = idMaps.get(entity.getMovieId());
            VideoMediaFile oneRow = oneRowList.get(0);
            oneRow.setFileOriginalName(entity.getMovieName());
        }
        info.setList(idLists);
        return idLists;
    }


    @GetMapping("qn")
    public Object uploadFile(String id) {
        FileRecord fileRecord = fileRecordService.getById(id);
        VideoMediaFile mediaFile = videoMediaFileService.getById(fileRecord.getMd5Value());
        // 上传到七牛云
        fileRecordService.uploadM3u8FileToQiNiu(mediaFile, fileRecord);
        return "ok";
    }


}
